/**
 * Copyright (c) 2017-2020 四川智慧云天 All rights reserved.
 *
 * https://www.sczhyt.cn
 *
 * 版权所有，侵权必究！
 */

package cn.sczhyt.modules.app.controller;


import cn.sczhyt.common.utils.R;
import cn.sczhyt.common.validator.Assert;
import cn.sczhyt.modules.app.annotation.Login;
import cn.sczhyt.modules.app.annotation.LoginUser;
import cn.sczhyt.modules.app.dao.UserDao;
import cn.sczhyt.modules.app.entity.UserEntity;
import cn.sczhyt.modules.app.form.LoginForm;
import cn.sczhyt.modules.app.service.UserService;
import cn.sczhyt.modules.app.utils.JwtUtils;
import cn.sczhyt.modules.sys.dao.SysUserRoleDao;
import cn.sczhyt.modules.sys.entity.SysUserRoleEntity;
import cn.sczhyt.modules.sys.form.PasswordForm;
import cn.sczhyt.modules.sys.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

/**
 * APP登录授权
 *
 * @author Mark sunlightcs@gmail.com
 */
@RestController
@RequestMapping("/app")
@Api(tags = "APP登录接口")
public class AppLoginController {

    @Autowired
    private SysUserRoleDao userRoleDao;

    @Autowired
    private UserService userService;
    @Autowired
    private JwtUtils jwtUtils;

    @Autowired
    private SysUserService sysUserService;
    /**
     * 登录
     */
    @PostMapping("/login")
    @ApiOperation("登录")
    public R login(@RequestBody LoginForm form){
        System.out.println(form);

        //用户登录
        UserEntity user = userService.login(form);

        //获取用户roleId
        SysUserRoleEntity userRoleEntity = userRoleDao.selectByUserId(user.getUserId());

        //生成token
        String token = jwtUtils.generateToken(user.getUserId(),user.getUsername(),userRoleEntity.getRoleId());

        Map<String, Object> map = new HashMap<>();
        map.put("token", token);
        map.put("expire", jwtUtils.getExpire());



        return R.ok(map);
    }


    /**
     * 退出账号
     */
    @Login
    @PostMapping("/logout")
    @ApiOperation("退出")
    public R logout(@LoginUser UserEntity user){

        //用户退出生成新token
        jwtUtils.generateToken(user.getUserId(),user.getUsername(),null);

        return R.ok();
    }


    /**
     * 修改登录用户密码
     */
    @ApiOperation("修改登录用户密码")
    @PostMapping("/password")
    @Login
    public R password(@LoginUser UserEntity user, @RequestBody PasswordForm form){

        System.out.println("AppLoginController.password" + form);
        Assert.isBlank(form.getNewPassword(), "新密码不为能空");

        //sha256加密
        String password = new Sha256Hash(form.getPassword(), user.getSalt()).toHex();
        //sha256加密
        String newPassword = new Sha256Hash(form.getNewPassword(), user.getSalt()).toHex();

        //更新密码
        boolean flag = sysUserService.updatePassword(user.getUserId(), password, newPassword);

        if(!flag){
            return R.error(10086,"原密码不正确");
        }
        return R.ok();
    }
}
